package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;
import com.gulimall.product.dao.SkuInfoDao;
import com.gulimall.product.entity.SkuInfoEntity;
import com.gulimall.product.service.SkuInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //TODO le,ge
        QueryWrapper<SkuInfoEntity> queryWrapper=new QueryWrapper<>();
        /**
         * page: 1
         * limit: 10
         * key:
         * catelogId: 0
         * brandId: 0
         * min: 0
         * max: 0
         */
        String key= (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and(query->{
                query.eq("sku_id",key).or().like("sku_name",key);
            });
        }

        String catalog_id = (String) params.get("catalog_id");
        if(!StringUtils.isEmpty(catalog_id)&&!"0".equalsIgnoreCase(catalog_id)){
            queryWrapper.eq("catalog_id",catalog_id);
        }

        String min= (String) params.get("min");
        if(!StringUtils.isEmpty(min)&&!"0".equalsIgnoreCase(min)){
            queryWrapper.ge("price",min);
        }

        String max= (String) params.get("max");
        if(!StringUtils.isEmpty(max)&&!"0".equalsIgnoreCase(max)){
            queryWrapper.le("price",max);
        }

        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId)&&!"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brandId",brandId);
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }


}